package pro.reznick.geoalarm;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class GeoAlarmActivity extends GeoAlarmBaseActivity
{

    private LayoutInflater inflater;
    private AlarmDataAdapter alarmsAdapter;
    private boolean initAlarmsAdapterOnControllerBound;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_n);
        final Context context = this.getApplicationContext();


        ListView lView = (ListView) findViewById(R.id.lstAlarms);
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View header = inflater.inflate(R.layout.alarm_list_header, null);
        lView.addHeaderView(header);
        registerForContextMenu(lView);

        Button button = (Button) findViewById(R.id.btn_add);
        button.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {
                startManagerAlarmActivityAsAdd();
            }

        });

    }



    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);

        if (v.getId() == R.id.lstAlarms)
        {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.alarm_context_menu, menu);
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
            menu.setHeaderTitle(((AlarmData) alarmsAdapter
                    .getItem(info.position - 1)).getName());
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item)
    {

        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
                .getMenuInfo();
        AlarmData alarm = (AlarmData) alarmsAdapter.getItem(info.position - 1);
        switch (item.getItemId())
        {
            case R.id.edit_alarm:
                startManagerAlarmActivityAsEdit(alarm);
                break;
            case R.id.delete_alarm:
                controllerTrait.controller.deleteAlarm(alarm);
                alarmsAdapter.refresh(controllerTrait.controller.getAlarms());
                Toast t1 = Toast.makeText(this, String.format(
                        " Alarm %s was deleted", alarm.getName()),
                        Toast.LENGTH_LONG);
                t1.show();
                break;
            default:
                Toast t2 = Toast.makeText(this, item.getItemId(),
                        Toast.LENGTH_SHORT);
                t2.show();
                break;

        }
        return true;
    }

    private void startManagerAlarmActivityAsAdd()
    {
        Intent intent = new Intent(this, ManageAlarmActivity.class);
        intent.putExtra("action", GeoAlarmAction.ADD_ALARM);
        startActivity(intent);
    }

    private void startManagerAlarmActivityAsEdit(AlarmData alarm)
    {
        Intent intent = new Intent(this, ManageAlarmActivity.class);

        intent.putExtra("action", GeoAlarmAction.EDIT_ALARM);
        intent.putExtra("alarmId", alarm.getId());
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.alarm_picker_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle item selection
        switch (item.getItemId())
        {
            case R.id.menu_add_alarmd_item:
                startManagerAlarmActivityAsAdd();
                return true;
            case R.id.menu_exit_item:
                exitApp();
                return true;
            // case R.id.menu_about_item:
            // showAbout();
            // return true;
        }
        return false;
    }


    private void exitApp()
    {
        controllerTrait.controller.exitApp();
        finish();
    }

    @Override
    protected void onDestroy()
    {

        super.onDestroy();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
    }

    @Override
    protected void onRestart()
    {
        super.onRestart();
    }


    @Override
    protected void onResume()
    {

        if (controllerTrait.isBound)
            initAlarmsAdapter();
        else
            initAlarmsAdapterOnControllerBound = true;



        super.onResume();
    }

    @Override
    protected void onControllerBoundEvent()
    {
        if (initAlarmsAdapterOnControllerBound)
            initAlarmsAdapter();
    }

    private void initAlarmsAdapter()
    {
        if (alarmsAdapter != null)
        {
            alarmsAdapter.refresh(controllerTrait.controller.getAlarms());
        }
        else
        {
            alarmsAdapter = new AlarmDataAdapter(controllerTrait.controller.getAlarms(),
                    inflater, controllerTrait.controller);
            ListView lView = (ListView) findViewById(R.id.lstAlarms);
            lView.setAdapter(alarmsAdapter);
        }
    }

}